//
//  CheckBoxView.m
//  OutesIOS
//
//  Created by norman on 2018/12/8.
//  Copyright © 2018 norman. All rights reserved.
//

#import "CheckBoxView.h"
#import "UITapView.h"

@interface CheckBoxView()

@property (nonatomic,strong)UIImageView *ivStatus;
@property (nonatomic,strong)UILabel *lbTitle;


@end

@implementation CheckBoxView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        // 添加提示信息文字的label
        self.isSelected = NO;
        [self setupViews:frame];
    }
    return self;
}

- (void)setupViews:(CGRect)frame {
    UITapView *bgView = [[UITapView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    @weakObj(self);
    [bgView addTargetsBlock:^(id obj) {
        Weakself.isSelected = !Weakself.isSelected;
        if (Weakself.isSelected) {
            self.ivStatus.image = [UIImage imageNamed:@"checkbox"];
        } else {
            self.ivStatus.image = [UIImage imageNamed:@"checkbox-blank"];
        }
        if (Weakself.statusChanged) {
            Weakself.statusChanged(Weakself.isSelected);
        }
    }];
    [self addSubview:bgView];
    self.ivStatus = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"checkbox-blank"]];
    self.ivStatus.x = 5;
    self.ivStatus.centerY = self.height/2;
    [bgView addSubview:self.ivStatus];
    
    self.lbTitle = [UILabel labelSingleLineFontSize:15 content:@"title"];
    self.lbTitle.x = CGRectGetMaxX(self.ivStatus.frame) + 5;
    self.lbTitle.centerY = self.height/2;
    self.lbTitle.width = self.width - 15 - self.lbTitle.x;
    self.lbTitle.textColor = CM_PLACEHOLDER_TEXT_COLOR;
    [bgView addSubview:self.lbTitle];
}


- (void)setCheckBoxTitle:(NSString *)title andStatus:(BOOL)isChecked {
    self.lbTitle.text = title;
    self.isSelected = isChecked;
    if (isChecked) {
        self.ivStatus.image = [UIImage imageNamed:@"checkbox"];
    } else {
        self.ivStatus.image = [UIImage imageNamed:@"checkbox-blank"];
    }
}


@end
